﻿using NewLife.Data;
using NewLife.Net;
using System;
using System.Collections.Generic;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static NetClient.LogFile;

namespace NetClient;

public static class SocketRemoteHelper
{
    /// <summary>切分文件流为多个数据包发出，接收方按顺序组装</summary>
    /// <param name="session"></param>
    /// <param name="file"></param>
    /// <param name="compressed"></param>
    /// <returns></returns>
    public static Int32 SendLogFile(this ISocketRemote session, String file, Boolean compressed = false)
    {
        var rs = 0;
        file.AsFile().OpenRead(compressed, s =>
        {
            rs = session.SendFileMessages((FileStream)s);
        });

        return rs;
    }

    /// <summary>切分数据流为多个数据包消息进行发送，接收方按顺序组装</summary>
    /// <param name="session">会话</param>
    /// <param name="stream">数据流</param>
    /// <returns>拆分消息数</returns>
    public static Int32 SendFileMessages(this ISocketRemote session, FileStream stream)
    {
        var count = 0;

        // 缓冲区大小，要减去4字节标准消息头。BufferSize默认8k，能得到最大吞吐。如果网络质量较差，这里可使用TCP最大包1448
        var bufferSize = SocketSetting.Current.BufferSize;
        bufferSize -= 4;

        var buffer = new Byte[bufferSize];
        using (FileStream fileStream = new FileStream(stream.Name, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
        {
            while (true)
            {
                var rs = fileStream.Read(buffer, 0, buffer.Length);
                if (rs <= 0) break;

                // 打包数据，标准编码器StandardCodec将会在头部加上4字节头部，交给下层Tcp发出
                var pk = new Packet(buffer, 0, rs);
                session.SendMessage(pk);

                count++;
            }
        }

        return count;
    }
}
